﻿using System;
using System.Windows.Forms;
using l1cs_cl.client;
using l1cs_cl.client.receivepackets;
using l1cs_cl.client.transferpackets;

namespace l1cs_cl
{
    public partial class frmNewCharacter : Form
    {
        private static frmNewCharacter _Instance;
        public static frmNewCharacter getInstance
        {
            get
            {
                if ( _Instance == null || _Instance.IsDisposed )
                    _Instance = new frmNewCharacter();
                return _Instance;
            }
        }

        public frmNewCharacter()
        {
            InitializeComponent();
        }

        // キャラクターの初期パラメータ
        private int[] ORIGINAL_STR = new int[] { 13, 16, 11, 8, 12, 13, 11 };
        private int[] ORIGINAL_DEX = new int[] { 10, 12, 12, 7, 15, 11, 10 };
        private int[] ORIGINAL_CON = new int[] { 10, 14, 12, 12, 8, 14, 12 };
        private int[] ORIGINAL_WIS = new int[] { 11, 9, 12, 12, 10, 12, 12 };
        private int[] ORIGINAL_CHA = new int[] { 13, 12, 9, 8, 9, 8, 8 };
        private int[] ORIGINAL_INT = new int[] { 10, 8, 12, 12, 11, 11, 12 };
        private static int[] ORIGINAL_AMOUNT = new int[] { 8, 4, 7, 16, 10, 6, 10 };

        // キャラクター作成時の返答コード
        public const int REASON_OK = 0x02;
        public const int REASON_ALREADY_EXSISTS = 0x06;
        public const int REASON_INVALID_NAME = 0x09;
        public const int REASON_WRONG_AMOUNT = 0x15;

        // クラスのコンボボックスを変更したときの処理
        private void cmbClass_SelectedIndexChanged( object sender, EventArgs e )
        {
            int type = cmbClass.SelectedIndex;
            if ( type < 0 )
            {
                return;
            }
            nudStr.Value = ORIGINAL_STR[type];
            nudDex.Value = ORIGINAL_DEX[type];
            nudCon.Value = ORIGINAL_CON[type];
            nudWis.Value = ORIGINAL_WIS[type];
            nudCha.Value = ORIGINAL_CHA[type];
            nudInt.Value = ORIGINAL_INT[type];
            txtPoint.Text = Convert.ToString( ORIGINAL_AMOUNT[type] );

        }

        // ステータスの変更時の処理
        private void ValueChanged( object sender, EventArgs e )
        {
            int type = cmbClass.SelectedIndex;
            if ( type < 0 )
            {
                return;
            }
            int iStr = Convert.ToInt32( nudStr.Value );
            int iDex = Convert.ToInt32( nudDex.Value );
            int iCon = Convert.ToInt32( nudCon.Value );
            int iWis = Convert.ToInt32( nudWis.Value );
            int iCha = Convert.ToInt32( nudCha.Value );
            int iInt = Convert.ToInt32( nudInt.Value );
            int iOrgStr = ORIGINAL_STR[type];
            int iOrgDex = ORIGINAL_DEX[type];
            int iOrgCon = ORIGINAL_CON[type];
            int iOrgWis = ORIGINAL_WIS[type];
            int iOrgCha = ORIGINAL_CHA[type];
            int iOrgInt = ORIGINAL_INT[type];
            int amount = ORIGINAL_AMOUNT[type];

            amount -= iStr - iOrgStr;
            amount -= iDex - iOrgDex;
            amount -= iCon - iOrgCon;
            amount -= iWis - iOrgWis;
            amount -= iCha - iOrgCha;
            amount -= iInt - iOrgInt;
            txtPoint.Text = Convert.ToString( amount );
            isMakeButtonCheck();
        }

        // 作成ボタンを有効にするかどうかの処理
        private void isMakeButtonCheck()
        {
            int amount = -1;
            if ( txtPoint.Text.Length > 0 )
            {
                amount = Convert.ToInt32( txtPoint.Text );
            }
            if ( amount == 0 &&
                ( rdbFemale.Checked || rdbMale.Checked ) &&
                txtName.Text.Length > 0 )
            {
                btnMake.Enabled = true;
            }
            else
            {
                if ( btnMake.Enabled == true )
                {
                    btnMake.Enabled = false;
                }
            }
        }

        // フォームがアクティブになったときの処理
        private void NewCharacter_Activated( object sender, EventArgs e )
        {
            txtName.Text = "";
            cmbClass.SelectedIndex = -1;
            rdbMale.Checked = false;
            rdbFemale.Checked = false;
            nudStr.Value = 0;
            nudCon.Value = 0;
            nudDex.Value = 0;
            nudWis.Value = 0;
            nudInt.Value = 0;
            nudCha.Value = 0;
            txtPoint.Text = "";
            btnMake.Enabled = false;
            txtName.Focus();
        }
        public Character chardata;

        // 作成ボタンの処理(作成パケット送信)
        private void btnMake_Click( object sender, EventArgs e )
        {
            String charname = txtName.Text;
            int Type = cmbClass.SelectedIndex;
            int _sex;
            if ( rdbMale.Checked )
            {
                _sex = 0;
            }
            else
            {
                _sex = 1;
            }
            int iStr = (int)nudStr.Value;
            int iDex = (int)nudDex.Value;
            int iCon = (int)nudCon.Value;
            int iWis = (int)nudWis.Value;
            int iCha = (int)nudCha.Value;
            int iInt = (int)nudInt.Value;
            T_NewChar tNewChar = new T_NewChar( charname, Type, _sex, iStr, iDex, iCon, iWis, iCha, iInt );
            // キャラクター作成パケット送信
            PlayerThread.SendPackets( tNewChar.getBytes() );

            // 作成結果
            R_NewCharWrong pNewCharWrong = new R_NewCharWrong( PlayerThread.readPacketNoTry() );
            if ( pNewCharWrong.result != REASON_OK )
            {
                if ( pNewCharWrong.result == REASON_ALREADY_EXSISTS )
                {
                    MessageBox.Show( "既に存在している名前です。" );
                }
                if ( pNewCharWrong.result == REASON_INVALID_NAME )
                {
                    MessageBox.Show( "名前が不正です。" );
                }
                if ( pNewCharWrong.result == REASON_WRONG_AMOUNT )
                {
                    MessageBox.Show( "キャラクタースロットが足りません" );
                }
                this.DialogResult = DialogResult.Cancel;
            }
            this.DialogResult = DialogResult.OK;
            Close();
        }

        // 男チェックボタンの処理
        private void rdbMale_CheckedChanged( object sender, EventArgs e )
        {
            isMakeButtonCheck();
        }

        // 女チェックボタンの処理
        private void rdbFemale_CheckedChanged( object sender, EventArgs e )
        {
            isMakeButtonCheck();
        }

        // 名前テキストボックスの処理
        private void txtName_TextChanged( object sender, EventArgs e )
        {
            isMakeButtonCheck();
        }

        // キャンセルボタンの処理
        private void btnCancel_Click( object sender, EventArgs e )
        {
            Close();
        }
    }
}
